package com.example.demo.service;

import com.example.demo.bean.*;
import com.example.demo.dao.UserMapper;
import com.example.demo.repository.*;
import com.example.demo.utils.StringUtils;
import io.rong.RongCloud;
import io.rong.methods.user.User;
import io.rong.models.response.TokenResult;
import io.rong.models.user.UserModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author zhangbiao
 * @date 2020/6/25 10:21
 * @desc 描述
 */
@Transactional
@Service
public class UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    UserRepository userRepository;


    public UserBean addUser(UserBean userBean){
       return userRepository.save(userBean);
    }

    public void updateUser(UserBean userBean){
       userRepository.save(userBean);
    }

    public UserBean login(String account, String password,String spIdentity){
        // 如果一个值存在，isPresent()将会返回true 并且 get() 将会返回所对应的值.

        UserBean userBean = userRepository.findByAccountAndPassword(account, password,spIdentity);
        if (userBean!=null){
            return userBean;
        }else {
            return null;
        }


    }


    public UserBean findByObjId(String userId){
        return userRepository.findByObjId(userId);
    }

    public UserBean findByAccount(String account){
        return userRepository.findByAccount(account);
    }

    public void deleteByUserObjId(String obj_id){
        userRepository.deleteByObjId(obj_id);
    }

    public List<Map<String, Object>> findTeachers(){
        return userRepository.findTeachers();
    }

    /**
     * 分页查询用户人员列表
     * @param page
     * @param pagesize
     * @return
     */
    public List<Map<String, Object>> userList(int page, int pagesize){
        Sort.Order order = new Sort.Order(Sort.Direction.DESC, "create_time");
        Pageable pageable = PageRequest.of(page, pagesize, Sort.by(order));
        List<Map<String, Object>> list = userRepository.findAllUser(pageable).getContent();
        return list;
    }

    public int findStudentCount(){
        return userRepository.findStudentCount();
    }

}
